package messageAPI.Tests;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.QueueingConsumer;

import java.io.IOException;

/**
 * Created with IntelliJ IDEA.
 * User: Elmar
 * Date: 2012/05/10
 * Time: 8:57 AM
 * To change this template use File | Settings | File Templates.
 */
public class TestUtilities {

    private final static String QUEUE_NAME = "javamessageapiqueue";

    public static long startTime;
    public static Connection testConnection;

    public static QueueingConsumer testConsumer;
    public static Channel testChannel;

    static void startTimer()
    {
        startTime=System.currentTimeMillis();
    }

    static void stopTimer()
    {
        long endTime = System.currentTimeMillis();
        System.out.printf("Test took %.3fs\n", (float)(endTime - startTime)/1000);
        startTime=0;
    }

    static void createRabbitChannel()
    {
        try
        {

            System.out.println(" [MessageAPITest] Test Startup - Creating Connection...");

            ConnectionFactory factory = new ConnectionFactory();
            factory.setHost("localhost");

            testConnection=null;
            testConnection = factory.newConnection();

            System.out.println(" [MessageAPITest] Test Startup - Connection Created.\t\t DONE!");

            System.out.println(" [MessageAPITest] Test Startup - Creating Channel...");
            testChannel=null;
            testChannel = testConnection.createChannel();

            System.out.println(" [MessageAPITest] Test Startup - Channel Created.\t\t DONE!");


            System.out.println(" [MessageAPITest] Test Startup - Declaring Queue on Channel...");
            testChannel.queueDeclare(QUEUE_NAME, false, false, false, null);

            System.out.println(" [MessageAPITest] Test Startup - Queue Declared.\t\t DONE!");
        }
        catch(IOException iox)
        {
            iox.printStackTrace();
        }
    }

    static void shutDownTests()
    {
        try {
            System.out.println(" [MessageAPITest] Shutting down test.");

            if(testChannel.isOpen())
            {
                System.out.println(" [MessageAPITest] Test ShutDown - Closing Channel.");
                testChannel.close();
                System.out.println(" [MessageAPITest] Test ShutDown - Channel Closed.\t\t DONE!");
            }

            if(testConnection.isOpen())
            {
                System.out.println(" [MessageAPITest] Test ShutDown - Closing Connection.");
                testConnection.close();

                System.out.println(" [MessageAPITest] Test ShutDown - Connection Closed.\t\t DONE!");
            }

            System.out.println(" [MessageAPITest] Shut down completed.\t\t DONE!");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    static void setupTests()
    {
        System.out.println(" [MessageAPITest] Test setup started.");

        createRabbitChannel();

        System.out.println(" [MessageAPITest] Test Startup - Creating Consumer...");
        testConsumer = new QueueingConsumer(testChannel);
        System.out.println(" [MessageAPITest] Test Startup - Consumer Created.\t\t DONE!");



        try {
            System.out.println(" [MessageAPITest] Test Startup - Consumer - Configure Channel Consumption...");

            testChannel.basicConsume(QUEUE_NAME, true, testConsumer);

            System.out.println(" [MessageAPITest] Test Startup - Consumer - Channel Consumption Configured.\t\t DONE!");

            System.out.println(" [MessageAPITest] Test setup completed.\t\t DONE!");

        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
